﻿using System;

namespace UniRx.Operators
{
	// Token: 0x020002E4 RID: 740
	internal class ScanObservable<TSource> : OperatorObservableBase<TSource>
	{
		// Token: 0x06000FC1 RID: 4033 RVA: 0x00048E78 File Offset: 0x00047278
		public ScanObservable(IObservable<TSource> source, Func<TSource, TSource, TSource> accumulator) : base(source.IsRequiredSubscribeOnCurrentThread<TSource>())
		{
			this.source = source;
			this.accumulator = accumulator;
		}

		// Token: 0x06000FC2 RID: 4034 RVA: 0x00048E94 File Offset: 0x00047294
		protected override IDisposable SubscribeCore(IObserver<TSource> observer, IDisposable cancel)
		{
			return this.source.Subscribe(new ScanObservable<TSource>.Scan(this, observer, cancel));
		}

		// Token: 0x0400092D RID: 2349
		private readonly IObservable<TSource> source;

		// Token: 0x0400092E RID: 2350
		private readonly Func<TSource, TSource, TSource> accumulator;

		// Token: 0x020002E5 RID: 741
		private class Scan : OperatorObserverBase<TSource, TSource>
		{
			// Token: 0x06000FC3 RID: 4035 RVA: 0x00048EA9 File Offset: 0x000472A9
			public Scan(ScanObservable<TSource> parent, IObserver<TSource> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
				this.isFirst = true;
			}

			// Token: 0x06000FC4 RID: 4036 RVA: 0x00048EC4 File Offset: 0x000472C4
			public override void OnNext(TSource value)
			{
				if (this.isFirst)
				{
					this.isFirst = false;
					this.accumulation = value;
				}
				else
				{
					try
					{
						this.accumulation = this.parent.accumulator(this.accumulation, value);
					}
					catch (Exception error)
					{
						try
						{
							this.observer.OnError(error);
						}
						finally
						{
							base.Dispose();
						}
						return;
					}
				}
				this.observer.OnNext(this.accumulation);
			}

			// Token: 0x06000FC5 RID: 4037 RVA: 0x00048F60 File Offset: 0x00047360
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000FC6 RID: 4038 RVA: 0x00048F98 File Offset: 0x00047398
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0400092F RID: 2351
			private readonly ScanObservable<TSource> parent;

			// Token: 0x04000930 RID: 2352
			private TSource accumulation;

			// Token: 0x04000931 RID: 2353
			private bool isFirst;
		}
	}
}
